﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Utility
{
    public class ChatMessage : BaseMessage
    {
        public string Message;
        public string SendTo;

        public ChatMessage()
        {
        }

        public ChatMessage(byte[] data)
        {
            int offset = 0;
            int sizeSendto = BitConverter.ToInt32(data, offset);
            offset += 4;
            SendTo = Encoding.UTF8.GetString(data, offset, sizeSendto);
            offset += sizeSendto;
            int sizeMessage = BitConverter.ToInt32(data, offset);
            offset += 4;
            Message = Encoding.UTF8.GetString(data, offset, sizeMessage);
        }

        public ChatMessage(string data)
            : this(Encoding.UTF8.GetBytes(data))
        {
        }

        public override byte[] ToByteArray()
        {
            var ls = new List<byte>();
            ls.AddRange(BitConverter.GetBytes(SendTo.Length));
            ls.AddRange(Encoding.UTF8.GetBytes(SendTo));
            ls.AddRange(BitConverter.GetBytes(Message.Length));
            ls.AddRange(Encoding.UTF8.GetBytes(Message));
            return ls.ToArray();
        }

        public override string ToString()
        {
            return Encoding.UTF8.GetString(ToByteArray());
        }
    }
}